package com.zl.basis.view;

import android.text.TextUtils;
import android.view.View;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class ZViewUtil {

    public static void setTextViewText(TextView textView, String text) {
        if (TextUtils.isEmpty(text) || null == textView) {
            return;
        }
        textView.post(() -> textView.setText(text));
    }

    public static String getTextViewText(TextView textView) {
        if (null == textView || null == textView.getText() || textView.getText().length() < 1) {
            return null;
        }
        return textView.getText().toString();
    }

    public static void viewRunUi(View view, Runnable runnable) {
        if (null != view && null != runnable) {
            view.post(runnable);
        }
    }

    public static void viewRunUi(View view, Runnable runnable, long delayMillis) {
        if (null != view && null != runnable) {
            view.postDelayed(runnable, delayMillis);
        }
    }

    public static void setSpinnerItemSelectedByValue(Spinner spinner, String value) {
        SpinnerAdapter apsAdapter = spinner.getAdapter();
        int k = apsAdapter.getCount();
        for (int i = 0; i < k; i++) {
            if (value.equals(apsAdapter.getItem(i).toString())) {
                spinner.setSelection(i, true);
                break;
            }
        }
    }
}
